/**
 * 
 */
package cn.bigcontainer.alt.meta.service.impl;

import javax.annotation.Resource;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.meta.dao.MDao;
import cn.bigcontainer.alt.meta.dto.MForm;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.mapper.MAppMapper;
import cn.bigcontainer.alt.meta.po.MApp;
import cn.bigcontainer.alt.meta.service.MAppService;
import cn.bigcontainer.alt.meta.service.MFormService;
import lombok.extern.slf4j.Slf4j;

/**
 * @author cailei
 * @date 2017年7月15日
 */
@Service
@Transactional(rollbackFor = { Exception.class })
@Primary
@Slf4j
public class MFormServiceImpl implements MFormService {

	@Resource
	private MDao woDao;

	@Resource
	private MAppService woAppService;

	@Resource
	private MAppMapper appMapper;

	private MForm getForm (String appId, BigMap<String, Object> req) {
		MApp app = appMapper.selectById(appId);
		return new MForm(app, req);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see wos.engine.services.IWoFormService#loadCreateForm(java.lang.String,
	 * java.lang.String, java.util.Map)
	 */
	@Override
	public MViewDto loadCreateForm(String appId, String keyVal, BigMap<String, Object> req) {
		MForm form = getForm(appId, req);
		MViewDto f = form.getCreateView();
		MViewDto fnew = form.beforeLoadCreateForm(f, keyVal);
		if (fnew != null) {
			return fnew;
		}
		woAppService.loadCreateForm(form.getCreateView(), keyVal);
		form.afterLoadCreateForm(f, keyVal);
		return f;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wos.engine.services.IWoFormService#loadUpdateForm(java.lang.String,
	 * java.lang.String, java.util.Map)
	 */
	@Override
	public MViewDto loadUpdateForm(String appId, String keyVal, BigMap<String, Object> req) {
		MForm form = getForm(appId, req);
		MViewDto f = form.getUpdateView();
		MViewDto fnew = form.beforeLoadUpdateForm(f, keyVal);
		if (fnew != null) {
			return fnew;
		}
		woAppService.loadUpdateForm(f, keyVal);
		form.afterLoadUpdateForm(f, keyVal);
		return f;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wos.engine.services.IWoFormService#loadForm(java.lang.String,
	 * java.util.Map)
	 */
	@Override
	public MForm loadForm(String appId, BigMap<String, Object> req) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see wos.engine.services.IWoFormService#loadFormMeta(java.lang.String,
	 * java.util.Map)
	 */
	@Override
	public MForm loadFormMeta(String appId, BigMap<String, Object> req) {
		return getForm(appId, req);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * wos.engine.services.IWoFormService#submitCreateForm(java.lang.String,
	 * java.util.Map)
	 */
	@Override
	public MViewDto submitCreateForm(String appId, BigMap<String, Object> req) {
		MForm form = getForm(appId, req);
		MViewDto fv = form.getCreateView();
		MViewDto fnew = form.beforeSubmitCreateForm(fv);
		if (fnew != null) {
			return fnew;
		}
		woAppService.submitCreateForm(fv);
		form.afterSubmitCreateForm(fv);
		return fv;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * wos.engine.services.IWoFormService#submitUpdateForm(java.lang.String,
	 * java.util.Map)
	 */
	@Override
	public MViewDto submitUpdateForm(String appId, BigMap<String, Object> req) {
		MForm form = getForm(appId, req);
		MViewDto f = form.getUpdateView();
		woAppService.submitUpdateForm(f);
		return f;
	}
}
